package com.dingqb.androidproject.threeaidl;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.dingqb.androidproject.R;
import com.pateo.okhttp.IMyAidlInterface;
import com.pateo.okhttp.Students;

import java.util.List;

/**
 * Created by dingqb on 2018/5/29.
 * 使用第三方service
 */

public class AidlActivity extends Activity {
    private static final String TAG = "AidlActivity";
    private IMyAidlInterface mService;
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mService = IMyAidlInterface.Stub.asInterface(iBinder);
            Log.e(TAG, "onServiceConnected: 客户端 connect");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG, "onServiceDisconnected: 客户端disconnect");

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_3_aidl);
        initView();
        Intent intent = new Intent("com.pateo.aidl");// 第三方服务类
        Intent intent1 = new Intent(createExplicitFromImplicitIntent(this, intent));
        bindService(intent1, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private void initView() {
        tv_result = findViewById(R.id.tv_result);
    }

    public void startAdd(View view) {
        try {
            Log.e(TAG, "客户端startAdd: " + mService.add(11, 22));
            tv_result.setText(String.valueOf(mService.add(11, 22)));
        } catch (RemoteException e) {
            Log.e(TAG, "startAdd: " + e.getMessage());
        }
    }

    public void inStudent(View view) {
        try {
            String info1 = mService.inStudentInfo(new Students(1, "Jim", "090415", 18));
            Log.e(TAG, "客户端inStudent: " + info1);
        } catch (RemoteException e) {
            Log.e(TAG, "inStudent: " + e.getMessage());
        }
    }

    public void outStudent(View view) {
        try {
            String info2 = mService.outStudentInfo(new Students(2, "Lida", "090416", 17));
            Log.e(TAG, "客户端outStudent: " + info2);
        } catch (RemoteException e) {
            Log.e(TAG, "outStudent: " + e.getMessage());
        }
    }

    public void inOutStudent(View view) {
        try {
            String info3 = mService.inOutStudentInfo(new Students(3, "Tom", "090417", 16));
            Log.e(TAG, "客户端inOutStudent: " + info3);
        } catch (RemoteException e) {
            Log.e(TAG, "inOutStudent: " + e.getMessage());
        }
    }

    /**
     * 兼容Android5.0中service的intent一定要显性声明
     *
     * @param context
     * @param implicitIntent
     *
     * @return
     */
    public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
        // Retrieve all services that can match the given intent
        PackageManager pm = context.getPackageManager();
        //通过queryIntentActivities()方法，查询Android系统的所有具备ACTION_MAIN和CATEGORY_LAUNCHER
        //的Intent的应用程序，点击后，能启动该应用，说白了就是做一个类似Home程序的简易Launcher 。
        List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);

        // Make sure only one match was found
        if (resolveInfo == null || resolveInfo.size() != 1) {
            return null;
        }

        // Get component info and create ComponentName
        ResolveInfo serviceInfo = resolveInfo.get(0);
        String packageName = serviceInfo.serviceInfo.packageName;
        String className = serviceInfo.serviceInfo.name;
        ComponentName component = new ComponentName(packageName, className);

        // Create a new intent. Use the old one for extras and such reuse
        Intent explicitIntent = new Intent(implicitIntent);

        // Set the component to be explicit
        explicitIntent.setComponent(component);

        return explicitIntent;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }

    TextView tv_result;

}
